home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Collection of Tools & Utilities
/
Collection of Tools and Utilities.iso
/
c
/
cpptut22.zip
/
CHAP05.TXT
< prev
next >
Wrap
Text File
|
1992-01-20
|
45KB
|
944 lines
Chapter 5
ENCAPSULATION
As mentioned in Chapter 1, object oriented programming will seem
very unnatural to a programmer with a lot of procedural programming
experience. This chapter is the beginning of the definition of
object oriented programming, and we will study the topic of
encapsulation which is a "divide and conquer" technique. As we
stated earlier, there are a lot of new terms used with object
oriented programming. Don't be intimidated by the new terminology,
we will study the terms one at a time in a meaningful order.
Encapsulation is the process of forming objects which we will
discuss throughout this chapter. An encapsulated object is often
called an abstract data type and it is what object oriented
programming is all about. Without encapsulation, which involves
the use of one or more classes, there is no object oriented
programming. Of course there are other topics concerning object
oriented programming, but this is the cornerstone.
WHY BOTHER WITH ENCAPSULATION?
_________________________________________________________________
We need encapsulation because we are human, and humans make errors.
When we properly encapsulate some code, we actually build an
impenetrable wall to protect the contained code from accidental
corruption due to the silly little errors that we are all prone to
make. We also tend to isolate errors to small sections of code to
make them easier to find and fix. We will have a lot more to say
about the benefits of encapsulation as we progress through the
tutorial.
NO INFORMATION HIDING
_________________________________________________________________
The program named OPEN.CPP is a really stupid ==============
program because it does next to nothing, but it OPEN.CPP
will be the beginning point for our discussion ==============
of encapsulation, otherwise known as information
hiding. Information hiding is an important part
of object oriented programming and you should have a good grasp of
what it is by the time we finish this chapter.
A very simple structure is defined in lines 4 through 6 which
contains a single int type variable within the structure. This is
sort of a silly thing to do but it will illustrate the problem we
wish to overcome in this chapter. Three variables are declared in
line 10, each of which contains a single int type variable and each
Page 5-1
Chapter 5 - Encapsulation
of the three variables are available anywhere within the main
function. Each variable can be assigned, incremented, read,
modified, or have any number of operations performed on it. A few
of the operations are illustrated in lines 13 through 21 and should
be self explanatory to anyone with a little experience with the C
programming language.
An isolated local variable named piggy is declared and used in the
same section of code to illustrate that there is nothing magic
about this code.
Study this simple program carefully because it is the basis for
beginning our study of encapsulation. Be sure to compile and
execute this program, then we will go on to the next example
program.
INFORMATION HIDING
_________________________________________________________________
Examine the program named CLAS.CPP for our first ==============
example of a program with a little information CLAS.CPP
hiding contained in it. This program is ==============
identical to the last one except for the way it
does a few of its operations. We will take the
differences one at a time and explain what is happening here. Keep
in mind that this is a trivial program and the safeguards built
into it are not needed for such a simple program but are used here
to illustrate how to use these techniques in a larger much more
complicated program.
The first difference is that we have a class instead of a structure
beginning in line 4 of this program. The only difference between
a class and a structure is that a class begins with a private
section whereas a structure has no private section automatically
defined. The keyword class is used to declare a class as
illustrated here.
The class named one_datum is composed of the single variable named
data_store and two functions, one named set() and the other named
get_value(). A more complete definition of a class is a group of
variables and one or more functions that can operate on that data.
Stay with us, we will tie this all together in a meaningful and
useful way very soon.
WHAT IS A PRIVATE SECTION?
_________________________________________________________________
A private section of a class is a section of data which cannot be
accessed outside of the class, it is hidden from any outside
access. Thus, the variable named data_store which is a part of the
object (an object will be defined completely later) named dog1
declared in line 23 is not available for use anywhere in the main
Page 5-2
Chapter 5 - Encapsulation
program. It is as if we have built a "brick wall" around the
variables to protect them from accidental corruption by outside
programming influences. It seems a little dumb to declare a
variable in the main program that we cannot use, but that is
exactly what we did.
Figure 5-1 is a graphical representation of the class with its
"brick wall" built around the data to protect it. You will notice
the small peep holes we have opened up to allow the user to gain
access to the functions. The peep holes were opened by declaring
the functions in the public section of the class.
WHAT IS A PUBLIC SECTION?
_________________________________________________________________
A new keyword, public, is introduced in line 6 which states that
anything following this keyword can be accessed from outside of
this class. Because the two functions are defined following the
keyword public, they are both public and available for use in the
calling function or any other function that is within the scope of
the calling function. This opens two small peepholes in the solid
wall of protection. You should keep in mind that the private
variable is not available to the calling program. Thus, we can
only use the variable by calling one of the two functions defined
as a part of the class. These are called member functions because
they are members of the class.
Since we have declared two functions, we need to define them by
saying what each function will actually do. This is done in lines
11 through 19 where they are each defined in the normal way, except
that the class name is prepended onto the function name and
separated from it by a double colon. These two function
definitions are called the implementation of the functions. The
class name is required because we can use the same function name
in other classes and the compiler must know with which class to
associate each function implementation.
One of the key points to be made here is that the private data
contained within the class is available within the implementation
of the member functions of the class for modification or reading
in the normal manner. You can do anything with the private data
within the function implementations which are a part of that class,
but the private data of other classes is hidden and not available
within the member functions of this class. This is the reason we
must prepend the class name to the function names of this class
when defining them.
It would be well to mention at this point that it is legal to
include variables and functions in the private part and additional
variables and functions in the public part. In most practical
situations, variables are included in only the private part and
functions are included in only the public part of a class
definition. Occasionally, variables